<template>
  <div>
    <page-tools :show-before="true">
      <span slot="before">共{{ dataList.length }}条记录</span>
      <template slot="chenck">
        <el-form label-width="120px">
          <!-- 部门  -->
          <el-form-item label="部门:">
            <el-checkbox-group v-model="checkedDepartment" @change="runFilter">
              <el-checkbox
                v-for="(item,index) in departmentList"
                :key="index"
                :label="item.name"
              />
            </el-checkbox-group>
          </el-form-item>

          <!-- 状态 -->
          <el-form-item label="考勤状态:">
            <el-checkbox-group v-model="checkedType" @change="runFilter">
              <el-checkbox
                v-for="item in attendanceData.holidayType"
                :key="item.id"
                :label="item.id"
              >{{ item.value }}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-form>
      </template>
    </page-tools>
  </div>
</template>
<script>
// 模拟数据
// import { getAttendancesListMock } from '@/api/attendances'
// 枚举数据
import attendanceData from '@/api/constant/attendance'
// 部门获取
import { getDepartments } from '@/api/departments'
export default {
  props: {
    dataList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      // 筛选状态
      attendanceData,
      // 绑定状态
      checkedType: [],
      // 绑定部门
      checkedDepartment: [],
      // 筛选部门
      departmentList: []
    }
  },
  async created() {
    const { depts } = await getDepartments()
    this.departmentList = depts
  },
  methods: {
    runFilter() {
      const res = this.changeDept(this.dataList)
      const filterList = this.changeType(res)
      this.$emit('changeData', filterList)
    },
    changeDept(data) {
      const filterList = this.checkedDepartment.length === 0 ? data : data.filter(item => this.checkedDepartment.includes(item.departmentName))
      return filterList
    },
    changeType(data) {
      const filterList = data.map(user => {
        return {
          ...user,
          attendanceRecord: this.coverRecord(user.attendanceRecord)
        }
      })
      return filterList
    },
    coverRecord(dayArr) {
      return dayArr.map(day => {
        return {
          ...day,
          adtStatu: (this.checkedType.length === 0 || this.checkedType.includes(day.adtStatu.toString())) ? day.adtStatu : ''
        }
      })
    }
  }
}
</script>

<style>

</style>
